/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2023-05-21
 * Time: 7:40
 */
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
        //test01();
//        test02();
//        test03();
//        test04();
//        test05();
//        test06();
//        test07();
//          test08();
        page01();
    }

    private static void page01() {
        WebDriver webDriver = new ChromeDriver(new ChromeOptions());
        webDriver.get("http://localhost:63342/deom1/src/main/Page/test01.html?_ijt=vn25nlnv5hafgoltniq8e1j5q4&_ij_reload=RELOAD_ON_SAVE");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.DAYS);
        List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));
        for (int i = 0; i < webElements.size(); i++) {
            //如果每个元素type值等于checkbok进行点击
            //getAttribute获取页面上的元素属性值,里面的type是当前元素属性
            if(webElements.get(i).getAttribute("type").equals("checkbox")){
                webElements.get(i).click();
            }
        }
    }

    private static void test08() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver(new ChromeOptions());
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //找到图片按钮
        WebElement webElement = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
        //鼠标右击
        Actions actions = new Actions(webDriver);
        sleep(3000);
        actions.moveToElement(webElement).contextClick().perform();
    }

    private static void test07() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver(new ChromeOptions());
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        //ctrl + A
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"A");
        sleep(3000);
        //ctrl + X
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"X");
        sleep(3000);
        //ctrl + V
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"V");

    }

    private static void test06() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver(new ChromeOptions());
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("D://20230521jietu.png"));

    }

    private static void test05() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver(new ChromeOptions());
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        //通过getWindowHandle获取所有的窗口句柄
        //通过getWindowHandle获取的get打开的页面窗口句柄
        System.out.println(webDriver.getWindowHandle());
        Set<String> handles = webDriver.getWindowHandles();
        String target_handle = "";
        for (String h : handles) {
            target_handle = h;
        }
        webDriver.switchTo().window(target_handle);
        sleep(3000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();

    }

    private static void test04() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver(new ChromeOptions());
        webDriver.get("https://www.baidu.com");
        sleep(3000);
        webDriver.quit();
        webDriver.close();
    }

    private static void test03() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver(new ChromeOptions());
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //浏览器后退
        webDriver.navigate().back();
        sleep(3000);
        //浏览器刷新
        webDriver.navigate().refresh();
        sleep(3000);
        //浏览器前进
        webDriver.navigate().forward();
        sleep(3000);
        //浏览器滚动条操作--进入底部
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        sleep(3000);
//        webDriver.manage().window().maximize();
        webDriver.manage().window().fullscreen();
        sleep(3000);
        webDriver.manage().window().setSize(new Dimension(600,1000));
    }

    private static void test02() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver(new ChromeOptions());
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).submit();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
//        sleep(3000);
        System.out.println(webDriver.findElement(By.cssSelector("#su")).getAttribute("value"));
        //清空百度搜索输入框中的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

    private static void test01() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //找到百度搜索输入框
//        WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        element.sendKeys("软件测试");
        //找到百度一下按钮
        //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for (int i = 0; i < elements.size(); i++) {
            if(!elements.get(i).getText().contains("软件测试")){
                System.out.println(elements.get(i).getText());
                System.out.println("测试不通过");
                return;
            }
        }
        System.out.println("测试通过");
    }
}